package com.smart.stock.module.spider.jobSpiderWeb.today;

import com.mongodb.client.result.UpdateResult;
import com.smart.stock.common.util.DateUtils;
import com.smart.stock.common.util.spring.EntityUtil;
import com.smart.stock.common.util.spring.SpringContextUtil;
import com.smart.stock.module.GlobalConstant;
import com.smart.stock.module.maindata.entity.Stock;
import com.smart.stock.module.maindata.entity.StockCodeInfo;
import com.smart.stock.module.maindata.entity.StockPriceToday;
import com.smart.stock.module.maindata.entity.StockPriceToday;
import com.smart.stock.module.spider.AbstractHtmlSpider;
import com.smart.stock.module.spider.entity.WebPage;
import com.smart.stock.module.spider.util.RequestUtil;
import com.smart.stock.module.spider.util.TradingDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * 同花顺行业板块
 *
 **/
@Slf4j
@Component
public class StockPriceTodaySpider extends AbstractHtmlSpider<StockPriceToday> {
    String entityName = EntityUtil.getEntityNameByCode(StockPriceToday.class, null);

    //当前要处理的代码
    private LinkedList<Integer> list = new LinkedList<>();

    @Bean
    private void getCurrentPage() {
        for (int i = 1; i <= 230; i++) {
            list.push(i);
        }
    }

    public MongoTemplate getMongoTemp() {
        return SpringContextUtil.getBean(MongoTemplate.class);
    }

    @Override
    public WebPage crawlPage() {
        Integer i = list.pollLast();
        if (i == null) {
            return null;
        }
        this.useAgent = SpringContextUtil.getProperties("spider.useagent");
        this.pageUrl = String.format(GlobalConstant.URL_THS_STOCK_TODAY,i);
        this.referer = GlobalConstant.URL_THS_INDUSTRY_REFERRER;
        this.method = Connection.Method.GET;
        Map<String, String> cookie = RequestUtil.string2token(GlobalConstant.URL_THS_INDUSTRY_COOKIESTR);
        this.cookie = cookie;
        return super.crawlPage();
    }

    @Override
    public void parsePage(WebPage webPage) throws Exception {
        if (null == webPage) {
            return;
        }
        String regex = ".*\\：|\\s*";
        Document doc = webPage.getDocument();
//        序号	板块	涨跌幅(%)	总成交量（万手）	总成交额（亿元）	净流入（亿元）	上涨家数	下跌家数	均价	领涨股	最新价	涨跌幅(%)


        /**
         * <tr>
         *  <td>61</td>
         *  <td><a href="http://stockpage.10jqka.com.cn/603217/" target="_blank">603217</a></td>
         *  <td><a href="http://stockpage.10jqka.com.cn/603217/" target="_blank">元利科技</a></td>
         *  <td class="c-rise">45.24</td>
         *  <td class="c-rise">9.99</td>
         *  <td class="c-rise">4.11</td>
         *  <td class="">0.00</td>
         *  <td>7.43</td>
         *  <td class="c-rise">1.92</td>
         *  <td class="c-rise">11.50</td>
         *  <td>1.49亿</td>
         *  <td>4569.94万</td>
         *  <td>20.67亿</td>
         *  <td>16.41</td>
         *  <td><a class="j_addStock" title="加自选" href="javascript:void(0);"><img src="http://i.thsi.cn/images/q/plus_logo.png" alt=""></a></td>
         * </tr>
         */
//        todo 1序号	2代码	3名称	4现价	5涨跌幅(%)	6涨跌	7涨速(%)	8换手(%)	9量比	10振幅(%)
//        todo 11成交额	12流通股	13流通市值	14市盈率

        Elements trs = doc.select("tbody").select("tr");
        getMongoTemp().remove(StockPriceToday.class);
        for (int i = 0; i < trs.size(); i++) {
            Element tr = trs.get(i);
            Query query = new Query(Criteria.where("_id").is(tr.select("td").get(2).text()));
            Update update = new Update();
            update.set("name", tr.select("td").get(3).text());
            update.set("price", tr.select("td").get(4).text());
            update.set("pctchg", tr.select("td").get(5).text());
            update.set("turnoverrate", tr.select("td").get(8).text());
            update.set("volRate", tr.select("td").get(9).text());
            update.set("fluctuate", tr.select("td").get(10).text());
            update.set("amount", tr.select("td").get(11).text());
            //更新时间
            update.set("priceDate", DateUtils.current_date_yyyymmdd());

            getMongoTemp().upsert(query,update, "stock");

        }




    }


}
